"""
核验记录模型
创建时间：2025-09-12
作者：开发团队
目的：定义访客核验记录数据模型
"""

from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Boolean, Text
from sqlalchemy.orm import relationship
from datetime import datetime, timezone
from ..core.database import Base


class VerificationRecord(Base):
    """访客核验记录模型"""
    
    __tablename__ = "verification_records"
    
    # 基础字段
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    visitor_name = Column(String(100), nullable=False)
    visitor_phone = Column(String(20), nullable=False, index=True)
    visitor_id_card = Column(String(20), index=True)
    visitor_company = Column(String(100))
    host_name = Column(String(100), nullable=False)
    host_phone = Column(String(20), nullable=False)
    visit_time = Column(DateTime, nullable=False)
    purpose = Column(String(200))
    verification_time = Column(DateTime, default=lambda: datetime.now(timezone.utc), nullable=False, index=True)
    guard_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True)
    result = Column(String(20), nullable=False, index=True)  # pass, reject
    rejection_reason = Column(Text)
    is_special = Column(Boolean, default=False, index=True)
    special_type = Column(String(20))  # VIP, 纠纷, 其他
    qr_data = Column(Text)
    notes = Column(Text)
    created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc), nullable=False)
    
    # 关系
    guard = relationship("User", back_populates="verification_records")
    
    def __repr__(self):
        return f"<VerificationRecord(id={self.id}, visitor='{self.visitor_name}', result='{self.result}')>"